<template>
  <div class="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 flex items-center justify-center p-4">
    <div class="max-w-md w-full">
      <!-- Logo和标题 -->
      <div class="text-center mb-8">
        <div class="inline-flex items-center justify-center w-16 h-16 bg-blue-600 rounded-full mb-4">
          <el-icon class="text-2xl text-white"><Car /></el-icon>
        </div>
        <h1 class="text-2xl font-bold text-gray-900 mb-2">车辆管理系统</h1>
        <p class="text-gray-600">请登录您的账户</p>
      </div>
      
      <!-- 登录表单 -->
      <el-card class="shadow-lg">
        <el-form 
          ref="loginFormRef"
          :model="loginForm" 
          :rules="loginRules"
          @submit.prevent="handleLogin"
        >
          <el-form-item prop="email">
            <el-input
              v-model="loginForm.email"
              type="email"
              placeholder="请输入邮箱"
              :prefix-icon="Message"
              size="large"
            />
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              size="large"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <div class="flex items-center justify-between w-full">
              <el-checkbox v-model="loginForm.remember">
                记住我
              </el-checkbox>
              <el-link type="primary" :underline="false">
                忘记密码？
              </el-link>
            </div>
          </el-form-item>
          
          <el-form-item>
            <el-button 
              type="primary" 
              size="large" 
              class="w-full"
              :loading="loading"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
        
        <!-- 快速登录选项 -->
        <div class="mt-6 pt-6 border-t border-gray-200">
          <p class="text-sm text-gray-600 text-center mb-4">快速登录测试账户</p>
          <div class="space-y-2">
            <el-button 
              size="small" 
              class="w-full" 
              @click="quickLogin('admin')"
            >
              管理员登录
            </el-button>
            <el-button 
              size="small" 
              class="w-full" 
              @click="quickLogin('employee')"
            >
              员工登录
            </el-button>
            <el-button 
              size="small" 
              class="w-full" 
              @click="quickLogin('driver')"
            >
              驾驶员登录
            </el-button>
          </div>
        </div>
      </el-card>
      
      <!-- 版权信息 -->
      <div class="text-center mt-8 text-sm text-gray-500">
        © 2024 车辆管理系统. 保留所有权利.
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage, type FormInstance } from 'element-plus'
import { Car, Message, Lock } from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 表单引用
const loginFormRef = ref<FormInstance>()

// 加载状态
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  email: '',
  password: '',
  remember: false
})

// 表单验证规则
const loginRules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 模拟登录API调用
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    // 模拟用户数据
    const userData = {
      id: '1',
      email: loginForm.email,
      name: '测试用户',
      role: 'admin' as const,
      phone: '13800138000'
    }
    
    userStore.login(userData)
    ElMessage.success('登录成功')
    router.push('/dashboard')
    
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}

// 快速登录
const quickLogin = (role: 'admin' | 'employee' | 'driver') => {
  const userData = {
    admin: {
      id: '1',
      email: 'admin@company.com',
      name: '系统管理员',
      role: 'admin' as const,
      phone: '13800138000'
    },
    employee: {
      id: '2',
      email: 'employee1@company.com',
      name: '李员工',
      role: 'employee' as const,
      phone: '13800138002'
    },
    driver: {
      id: '3',
      email: 'driver1@company.com',
      name: '张师傅',
      role: 'driver' as const,
      phone: '13800138001'
    }
  }
  
  userStore.login(userData[role])
  ElMessage.success(`${userData[role].name} 登录成功`)
  router.push('/dashboard')
}
</script>

<style scoped>
.el-card {
  border: none;
  border-radius: 12px;
}

.el-button {
  border-radius: 8px;
}

.el-input {
  border-radius: 8px;
}
</style>